﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AlexPilotti.FTPS.Client;
using AlexPilotti.FTPS.Common;
using System.Net;
using System.IO;
using YLPublicService;

namespace YLServerInterface.FunctionalClass
{
    public class FTP
    {
        private string host;
        private NetworkCredential credential;
        private FTPSClient client;
        public FTP(string host, string name, string pwd)
        {
            this.host = host;
            credential = new NetworkCredential(name, pwd);
            client = new FTPSClient();
        }
        public void DownloadFile(string remoteFileName, string outputFileName)
        {
            client.Connect(host, credential, ESSLSupportMode.ClearText);
            var fs = client.GetFile(remoteFileName);
            saveFile(fs, outputFileName);
        }

        private void saveFile(FTPStream input, string outputFileName)
        {
            FileStream fs;
            if (File.Exists(outputFileName) == false)
            {
                fs = File.Create(outputFileName);

            }
            else
            {
                fs = File.OpenWrite(outputFileName);
            }
            byte[] buff = new byte[1024];
            int num = input.Read(buff, 0, buff.Length);
            while (num != 0)
            {
                fs.Write(buff, 0, num);
                num = input.Read(buff, 0, buff.Length);
            }
            input.Close();
            fs.Close();
        }
    }
}
